package com.company.笔试.反转链表;

import java.util.Stack;

/**
 * @ClassName ReverseList
 * @Description TODO
 * @Author 11
 * @Date 2021/6/6 19:05
 * @Version 1.0
 */
class Node{
    int val;
    Node next;
    public Node(int val) {
        this.val = val;
    }
}




public class ReverseList {
    public static Node ReverseListSolution(Node head){
        Stack<Object> objects = new Stack<>();
        objects.lastElement();
        //定义tmp保存当前节点的下一节点
        Node tmp = null;
        //定义tail保存已反转的节点
        Node tail = null;
        //当前节点不为空
        while (head!=null){
            tmp = head.next;
            head.next = tail;
            tail = head;
            head=tmp;
        }



        return tail;
    }

    public static void main(String[] args) {
        Node head =  new Node(3);
        head.next = new Node(4);
        head.next.next = new Node(6);
        head.next.next.next = new Node(7);
        Node node = ReverseListSolution(head);
        while (node!=null){
            System.out.println(node.val);
            node=node.next;
        }
    }
}
